package kstech.com.wifitest.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import kstech.com.wifitest.R;
import kstech.com.wifitest.service.WifiService;
import kstech.com.wifitest.utils.BitmapUtils;
import kstech.com.wifitest.utils.ChartUtils;

public class WIFIActivity extends AppCompatActivity implements WifiService.onWIFIChangeListener {

    private WIFIActivity activity;
    private ImageView ivWiFi;
    private TextView tvWiFi;
    private TextView tvInfo;
    private TextView tvSocket;
    private TextView tvWiFiStrength;
    private ProgressBar pbWiFi;
    private WifiService wifiService;
    private LineChart mChart;
    private LineData data;
    private List<Entry> entries;
    private LineDataSet dataset;
    private WifiManager wifiManager;
    private int x = 5;
    private Socket sockTcp;
    private ServiceConnection serviceConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi);
        activity = this;
        wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        ivWiFi = (ImageView) findViewById(R.id.iv_wifi);
        tvWiFi = (TextView) findViewById(R.id.tv_wifi);
        tvInfo = (TextView) findViewById(R.id.tv_info);
        tvSocket = (TextView) findViewById(R.id.tv_socket);
        tvWiFiStrength = (TextView) findViewById(R.id.tv_strength);
        pbWiFi = (ProgressBar) findViewById(R.id.pb_wifi);
        mChart = (LineChart) findViewById(R.id.chart_wifi);
        chartinit();

    }

    public void chartinit(){
        //设置是否可以触摸，如为false，则不能拖动，缩放等
        mChart.setTouchEnabled(true);
        //设置是否可以拖拽，缩放
        mChart.setDragEnabled(true);
        mChart.setScaleEnabled(true);
        //设置是否能扩大扩小
        mChart.setPinchZoom(true);

        mChart.setDrawGridBackground(false);
        XAxis xAx = mChart.getXAxis();
        xAx.setAxisMinimum(0);
        xAx.setGridColor(R.color.trans);

        //设置x轴位置
        mChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);

        data = new LineData();
        entries = new ArrayList<>();
        entries.add(new Entry(0,0));
        entries.add(new Entry(1,2));
        entries.add(new Entry(2,6));
        entries.add(new Entry(3,5));
        entries.add(new Entry(4,99));
        dataset = new LineDataSet(entries,"wifi");
        dataset.setColor(Color.RED);
        dataset.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        // 不显示坐标点的小圆点
        dataset.setDrawCircles(false);
        // 不显示坐标点的数据
        dataset.setDrawValues(false);
        data.addDataSet(dataset);
        mChart.setData(data);
    }

    public void startwifilistener(View view) {
        Intent intent = new Intent(this, WifiService.class);
        startService(intent);
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                WifiService.MyBinder binder = (WifiService.MyBinder) service;
                wifiService = binder.getService();
                wifiService.setWifiListner(activity);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent, serviceConnection,BIND_AUTO_CREATE);
    }

    @Override
    public void signalChanged(final String wifissid,final int progress) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvWiFi.setText(wifissid);
                tvWiFiStrength.setText("信号强度："+progress);
                pbWiFi.setProgress(progress);
            }
        });

    }

    @Override
    public void statuesChanged(final String status) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvInfo.setText(status);
            }
        });
    }

    @Override
    public void forChart(int progress) {
        entries.add(new Entry(x++,progress));
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ChartUtils.notifyDataSetChanged(mChart,entries,0);
            }
        });

    }

    //给表格中新增一条数据
    public void chartshow(View view) {
        entries.add(new Entry(x++,80));
        //ChartUtils.setChartData();
        ChartUtils.notifyDataSetChanged(mChart,entries,0);
    }

    //socket 信息测试
    public void socketTest(View view) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    sockTcp = new Socket("192.168.0.178", 4001);
                    if(sockTcp == null){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvSocket.setText("socket null");
                            }
                        });
                    }else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvSocket.setText("socket normal常");
                            }
                        });
                    }
                } catch (final Exception e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvSocket.setText("socket 异常："+e.toString());
                        }
                    });
                }
            }
        },100,1000);
    }

    //线程暂停
    public void pause(View view) {
        Intent intent = new Intent(this, WifiService.class);
        wifiService.setWifiListner(null);
        wifiService.stopTimer();
        stopService(intent);
        unbindService(serviceConnection);
    }

    //图片保存
    public void savePIC(View view) {
        Bitmap saveBitmap = mChart.getChartBitmap();
        BitmapUtils.getInstance(this).saveBitmapForSdCard(saveBitmap,"/storage/sdcard1/test.JPEG");
    }
}
